package com.k1ccc0.alumniinformationmanagement.controller;

import com.k1ccc0.alumniinformationmanagement.pojo.Alumni;
import com.k1ccc0.alumniinformationmanagement.pojo.Result;
import com.k1ccc0.alumniinformationmanagement.service.AlumniService;
import com.k1ccc0.alumniinformationmanagement.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private AlumniService alumniService;


    @PostMapping("/Act/ImgUpload")
    public Result picUpload(MultipartFile image) throws IOException {
        log.info("图片上传，图片名:{}", image.getOriginalFilename());
        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        log.info("图片上传完成:{}", url);
        return Result.success(url);
    }

    @PostMapping("/AvatarUpdate/{username}")
    public Result avatarUpload(MultipartFile image, @PathVariable String username) throws IOException {
        log.info("图片上传，图片名:{}", image.getOriginalFilename());
        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        log.info("图片上传完成:{}", url);
        Alumni alumni = new Alumni();
        alumni.setUsername(username);
        alumni.setImage(url);
        alumniService.updateAvatar(alumni);
        return Result.success(url);
    }
}
